//
//  SEAuthViewModel.swift
//  SmartEyeForPatient
//
//  Created by qdsg on 2021/4/18.
//

import UIKit

class SEAuthViewModel: SEBaseViewModel {

    var loginModel = SELoginModel()
    var userInfoModel = SEUserInfoModel()

    convenience init(loginModel: SELoginModel) {
        self.init()
        self.loginModel = loginModel
    }

    convenience init(registerModel: SELoginModel) {
        self.init()
    }

    var account: String = ""
    var code: String = ""
    var oldPassword: String = ""
    var password: String = ""
    var confirmPassword: String = ""

    var isAgree: Bool = false

    var msgCodeHandler: ((SEHttpStatus) -> Void)?

    var loginHandler: ((SEHttpStatus) -> Void)?

    var findPsdHandler: ((SEHttpStatus) -> Void)?

    var changePsdHandler: ((SEHttpStatus) -> Void)?

    func saveAuthToken() {
        UserDefaults.saveAuthToken(loginModel.accessToken)
        SELog(UserDefaults.authToken)
    }
    func saveUserAccountInfo() {
        UserDefaults.userId = userInfoModel.userId
        UserDefaults.userNick = userInfoModel.nickName
        UserDefaults.userAvatar = userInfoModel.imgPath
        UserDefaults.videoAccount = userInfoModel.videoAccount
        if UserDefaults.remberAccountAndPsd {
            UserDefaults.userAccount = account
            UserDefaults.userPsd = password
        } else {
            UserDefaults.removeUserAccount()
            UserDefaults.removeUserPsd()
        }
    }
    func removeAuthToken() {
        UserDefaults.removeAuthToken()
    }

    var checkLogin: String? {
        if account.isEmpty {
            return "请输入手机号"
        }
        if password.isEmpty {
            return "请输入密码"
        }
        return nil
    }

    var checkNext: String? {
        if account.isEmpty {
            return "请输入手机号"
        }
        if code.isEmpty {
            return "请输入验证码"
        }
        if !isAgree {
            return "请阅读并同意患者须知"
        }
        return nil
    }

    var checkPassword: String? {
        if password.isEmpty || confirmPassword.isEmpty {
            return "请输入密码"
        }
        if password != confirmPassword {
            return "两次输入密码不一致"
        }
        return nil
    }

    var checkFindPsd: String? {
        if account.isEmpty {
            return "请输入手机号"
        }
        if code.isEmpty {
            return "请输入验证码"
        }
        if let checkPsd = checkPassword {
            return checkPsd
        }
        return nil
    }

    var checkChangePsd: String? {
        if oldPassword.isEmpty {
            return "请输入旧密码"
        }
        if let checkPsd = checkPassword {
            return checkPsd
        }
        return nil
    }


    /// APIRequest

    /// 获取验证码
    func requestMsgCode() {
        if account.isEmpty {
            SEHUD.showError(message: "请输入手机号")
            return
        }
        SEHttpManager.shared.requestForAny(SEAuthAPI.getMsg(phone: account), withLoading: true) { result, status in
            self.msgCodeHandler?(status)
        }
    }

    /// 登录
    func requestLogin() {
        if let msg = checkLogin {
            SEHUD.showError(message: msg)
            return
        }
        SEHttpManager.shared.request(SEAuthAPI.login(account: account, password: password), model: SELoginModel.self, withLoading: true) { (model, status) in
            switch status {
            case .success:
                self.loginModel = model ?? SELoginModel()
                self.saveAuthToken()
            default:
                break
            }
            self.loginHandler?(status)
        }
    }

    /// 获取用户信息
    func requestUserInfo() {
        SEHttpManager.shared.request(SEProfileAPI.userInfo, model: SEUserInfoModel.self, withLoading: true) { (model, status) in
            switch status {
            case .success:
                self.userInfoModel = model ?? SEUserInfoModel()
                self.saveUserAccountInfo()
                self.bindJPushRegistrationID()
            default:
                self.removeAuthToken()
            }
            self.updateData?(status)
        }
    }

    /// 注册
    func register() {
        if let msg = checkPassword {
            SEHUD.showError(message: msg)
            return
        }
        SEHttpManager.shared.requestForAny(SEAuthAPI.register(phone: account, code: code, password: confirmPassword), withLoading: true) { (result, status) in
            self.updateData?(status)
        }
    }

    /// 找回密码
    func requestFindPassword() {
        if let msg = checkFindPsd {
            SEHUD.showError(message: msg)
            return
        }
        SEHttpManager.shared.requestForAny(SEAuthAPI.findPassword(account: account, password: password, phone: account, code: code), withLoading: true) { retult, status in
            self.findPsdHandler?(status)
        }
    }

    /// 修改密码
    func requestChangePassword() {
        if let msg = checkChangePsd {
            SEHUD.showError(message: msg)
            return
        }
        SEHttpManager.shared.requestForAny(SEAuthAPI.changePassword(account: account, oldPassword: oldPassword, password: password), withLoading: true) { result, status in
            self.changePsdHandler?(status)
        }
    }

    func bindJPushRegistrationID() {
        SEAuthViewModel.bindJPushRegistrationID()
    }

}

extension SEAuthViewModel {
    static func bindJPushRegistrationID() {
        guard !UserDefaults.jpushRegistrationId.isEmpty else { return }
        SEHttpManager.shared.requestForAny(SEAuthAPI.bindPushAccount(devNum: UserDefaults.jpushRegistrationId)) { result, status in
            SELog("JPushRegistrationId------\(UserDefaults.jpushRegistrationId)------")
            SELog("BindResult------\(status)------")
        }
    }
}

